package cn.com.bod.fas.common.interceptor;

import java.lang.reflect.Field;
import java.util.Objects;

import cn.com.yusys.yusp.commons.util.BeanUtil;

public class AbstractInterceptor {

	public <T> T getValue(Object object, String name) throws Exception {
		
		String[] names = name.split("\\.");
		if (names.length == 1) {
			return (T) BeanUtil.getObjectFieldValue(object, this.getField(object, names[0]));
		} else {
			return getValue(object, names);
		}
		
		
	}
	
	public <T> T getValue(Object object, String[] names) throws Exception {
		Object o = object;
		for (String name : names) {
			o = getValue(o, name);
		}
		return (T) o;
	}
	
	
	public Field getField(Object obj, String fieldName) throws Exception {
		
		Field field = null;
		try {
			field = obj.getClass().getDeclaredField(fieldName);
		} catch (Exception e) {}
		return Objects.nonNull(field) ? field : obj.getClass().getSuperclass().getDeclaredField(fieldName);
		
		
	}
	
}
